package cqrtplm.aop;

import com.hustcad.plm.pdm.partbom.dto.PartBomTree;
import com.hustcad.plm.pdm.partbom.vo.PartBomTreeByFilterVO;
import cqrtplm.dto.query.RtSegmNumQueryDTO;
import cqrtplm.entity.RtSegmNumDO;
import cqrtplm.service.RtSegmNumService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Aspect
@Component
@Slf4j
public class BOMAttriblesTableAOP {
    @Resource
    private RtSegmNumService rtSegmNumService;
    @Value("${rtEnumName}")
    private String rtEnumName;
    /**
     * 通过切面增加批量设置关系属性时的自定义枚举
     * @param proceedingJoinPoint
     * @return Map<String, Map<String, String>>
     * @auth ding
     */

//    @SneakyThrows
//    @Around(value = "execution(* com.hustcad.plm.pdm.enumeration.service.impl.TyplmEnumerationServiceImpl.getEnabledEnumItemsMap(..))")
//    public Map<String, Map<String, String>> rtGetEnabledEnumItemsMap(ProceedingJoinPoint proceedingJoinPoint) {
//        log.info("通过切面增加批量设置关系属性时的枚举==================");
//        Object[] args = proceedingJoinPoint.getArgs();
//        log.info("方法参数: {}", args);
//        Object result = proceedingJoinPoint.proceed();
//        if (result instanceof Map) {
//            Map<String, Map<String, String>> map = (Map<String, Map<String, String>>) result;
//            List<RtSegmNumDO> list = rtSegmNumService.listAll(new RtSegmNumQueryDTO());
//            Map<String, String> data = new HashMap<>();
//            for (RtSegmNumDO rtSegmNumDO : list) {
//                String id = rtSegmNumDO.getId();
//                if (StringUtils.isNotBlank(id)) {
//                    data.put(id + "（" + rtSegmNumDO.getPosition() + "）", id + "（" + rtSegmNumDO.getPosition() + "）");
//                }
//            }
//            map.put(rtEnumName, data);
//            return map;
//        }
//        return (Map<String, Map<String, String>> ) result;
//
//    }

    /**
     * 通过切面修改批量设置BOM关系属性
     * 修改装配位置/段号input为select下拉框
     * @param proceedingJoinPoint
     * @return PartBomTreeByFilterVO
     * @auth ding
     */
    @SneakyThrows
    @Around(value = "execution(* com.hustcad.plm.pdm.partbom.service.TyplmPartBomService.expandBomTreeByFilterNew(com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO, java.lang.Boolean))")
    public PartBomTreeByFilterVO rtExpandBomTreeByFilterNew(ProceedingJoinPoint proceedingJoinPoint) {
        log.info("修改装配位置/段号input为select下拉框==================");
        Object[] args = proceedingJoinPoint.getArgs();
        log.info("方法参数: {}", args);
        Object result = proceedingJoinPoint.proceed();
        if (result instanceof PartBomTreeByFilterVO) {
            PartBomTreeByFilterVO partBomTreeByFilterVO = (PartBomTreeByFilterVO) result;
            List<PartBomTree> partBomTreeList = partBomTreeByFilterVO.getPartBomTreeList();
            if (partBomTreeList != null && partBomTreeList.size() > 1) {
                partBomTreeList.subList(1, partBomTreeList.size()).forEach(item -> {
                    Optional.ofNullable(item.getBomIba()).ifPresent(bomList -> bomList.forEach(part -> {
                        Optional.ofNullable(part.getConstraintForm()).ifPresent(forms -> forms.forEach(constraint -> {
                            if (rtEnumName.equals(constraint.getProp())) {
                                constraint.setType("tySelect");
                                constraint.setTypeParam(Collections.singletonList(rtEnumName));
                            }
                        }));
                    }));
                });
            }
            return partBomTreeByFilterVO;
        }
        // 如果返回值不是期望的类型，则直接返回原始结果
        return (PartBomTreeByFilterVO) result;
    }
}

